//
//  CAMerchantsRegisteredVC.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/6/3.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "CAMerchantsRegisteredVC.h"
#import "UseAgreementVC.h"
#import "NSString+Check.h"
#import "Friend.h"
#import "ChooseIndustryVC.h"

@interface CAMerchantsRegisteredVC ()<UITextFieldDelegate>{
    
    NSInteger repeatCount;
    NSTimer *timer;
    Friend *_chooseInd;
    
}

@property (weak, nonatomic) IBOutlet UIView *agreementVIew;
//手机
@property (weak, nonatomic) IBOutlet UIImageView *phoneImageView;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

//验证码
@property (weak, nonatomic) IBOutlet UIImageView *codeImageView;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

//密码
@property (weak, nonatomic) IBOutlet UIImageView *pwdImageView;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIImageView *pwdRemoveImageView;

//行业类别
@property (weak, nonatomic) IBOutlet UIImageView *typeImageView;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *selectTypeImageView;

//门店
@property (weak, nonatomic) IBOutlet UIImageView *storesImageView;
@property (weak, nonatomic) IBOutlet UITextField *storesTextField;
@property (weak, nonatomic) IBOutlet UIImageView *storesRemoveImageView;

//商场信息
@property (weak, nonatomic) IBOutlet UIImageView *shopInfoImageView;
@property (weak, nonatomic) IBOutlet UILabel *shopInfoLabel;
@property (weak, nonatomic) IBOutlet UIImageView *getMessageImageView;

//门店详细楼层位置
@property (weak, nonatomic) IBOutlet UIImageView *locationImageView;
@property (weak, nonatomic) IBOutlet UITextField *locationTextField;
@property (weak, nonatomic) IBOutlet UIImageView *removeLocationImageView;

@end

@implementation CAMerchantsRegisteredVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.registerType == INDIVIDUAL_BUSINESSES) {
        
        self.shopInfoLabel.text = @"点击获取门店街道信息";
        self.locationTextField.placeholder = @"请输入详细门牌号码";
    }
    
    repeatCount = 5;
    
    self.phoneTextField.delegate = self;
    self.codeTextField.delegate = self;
    self.pwdTextField.delegate = self;
    self.storesTextField.delegate = self;
    self.locationTextField.delegate = self;
    
//    添加手势
    
    [self.pwdRemoveImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeText:)]];
    
    [self.storesRemoveImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeText:)]];
    
    [self.agreementVIew addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(agreement)]];
    
    [self.typeLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseIndividual)]];
    [self.typeImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseIndividual)]];
    // Do any additional setup after loading the view from its nib.
}

- (void)agreement{
    
    UseAgreementVC *agreementVC = [[UseAgreementVC alloc] init];
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:agreementVC animated:YES];
    
}

#pragma mark -- 选择行业
- (void)chooseIndividual{
    
    ChooseIndustryVC *chooseInd = [[ChooseIndustryVC alloc] init];
    
    __weak __typeof(self) weakSelf = self;
    
    chooseInd.chooseIndustryVCBlock = ^(Friend *fri){
        
        _chooseInd = fri;
        weakSelf.typeLabel.text = _chooseInd.name;
        weakSelf.typeLabel.textColor = [UIColor blackColor];
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
    };
    
    [self.navigationController pushViewController:chooseInd animated:YES];
    
}

#pragma mark -- 清除文本
- (void)removeText:(UITapGestureRecognizer *)aTap{
    switch (aTap.view.tag) {
        case 12:
            self.pwdTextField.text = @"";
            break;
        case 13:
            self.storesTextField.text = @"";
            break;
        case 14:
            self.locationTextField.text = @"";
            break;
            
        default:
            break;
    }
}

#pragma mark -- 获取验证码
- (IBAction)getCode:(id)sender {
    
    if (![NSString isPhone:self.phoneTextField.text]) {
        return;
    }
    
    timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(veriCountDown:) userInfo:nil repeats:YES];
    
    self.codeBtn.backgroundColor =[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1];
    
    self.codeBtn.enabled = NO;
    
    // 不加这句不运行
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    
    NSString *urlStr = [NSString stringWithFormat:@"/Api/Public/sendSms?phone=%@&type=%d",self.phoneTextField.text,1];
    
    [HTTPManager get:GET_URLString(urlStr) success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if ([responseObject[@"msg"] isEqualToString:@"成功"]) {
            
            self.codeTextField.text = ((NSNumber *)responseObject[@"date"][@"send_param"][@"code"]).stringValue;
            
        }
        [self hideIndicator];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hideIndicator];
    }];
    
}

#pragma mark - 发送验证码倒计时

- (void)veriCountDown:(NSTimer *)sender {
    
    repeatCount--;
    
    NSString *timerText = [NSString stringWithFormat:@"%ld秒",(long)repeatCount];
    
    [self.codeBtn setTitle:timerText forState:UIControlStateNormal];
    
    // 如果倒计时到了0
    if (repeatCount == 0) {
        
        repeatCount = 5;
        [timer invalidate];
        timer = nil;
        
        // 按钮重新可以被点击
        self.codeBtn.enabled = YES;
        [self.codeBtn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
        self.codeBtn.titleLabel.text = @"获取验证码";
        CALayer *layer = self.codeBtn.layer;
        layer.borderColor = [UIColor colorWithRed:247/255.0 green:119/255.0 blue:0/255.0 alpha:1].CGColor;
        //背景颜色
        self.codeBtn.backgroundColor =[UIColor colorWithRed:74/255.0 green:148/255.0 blue:267/255.0 alpha:1];
        
    }
    
}

#pragma mark -- TextFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    switch (textField.tag) {
        case 10:{
            
            self.phoneImageView.image = [UIImage imageNamed:@"手机蓝.png"];
            break;
        }
        case 11:{
            self.codeImageView.image = [UIImage imageNamed:@"验证码蓝.png"];
            break;
        }
        case 12:{
            self.pwdImageView.image = [UIImage imageNamed:@"密码蓝.png"];
            self.pwdRemoveImageView.hidden = NO;
            break;
        }
        case 13:{
            
            self.storesImageView.image = [UIImage imageNamed:@"商户名蓝.png"];
            self.storesRemoveImageView.hidden = NO;
            break;
        }
        case 14:{
            
            self.locationImageView.image = [UIImage imageNamed:@"详细蓝.png"];
            self.removeLocationImageView.hidden = NO;
            break;
        }
        default:
            break;
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    switch (textField.tag) {
        case 10:{
            
            self.phoneImageView.image = [UIImage imageNamed:@"手机.png"];
            break;
        }
        case 11:{
            
            self.codeImageView.image = [UIImage imageNamed:@"验证码.png"];
            break;
        }
        case 12:{
            
            self.pwdImageView.image = [UIImage imageNamed:@"密码.png"];
            self.pwdRemoveImageView.hidden = YES;
            break;
        }
        case 13:{
            
            self.storesImageView.image = [UIImage imageNamed:@"商户名.png"];
            self.storesRemoveImageView.hidden = YES;
            break;
        }
        case 14:{
            
            self.locationImageView.image = [UIImage imageNamed:@"详细.png"];
            self.removeLocationImageView.hidden = YES;
            break;
        }
        default:
            break;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}


#pragma mark -- 确认提交
- (IBAction)submit:(id)sender {
    
    if (![NSString isPhone:self.phoneTextField.text]) {
        return;
    }
    
    if ([self.codeTextField.text isEqualToString:@""]) {
        [ShowMessage showCenterViewMessage:@"请输入验证码"];
        return;
    }
    
    if ([NSString isPwd:self.pwdTextField.text]) {
        return;
    }
    
    if ([self.typeLabel.text isEqualToString:@"请选择行业类别"]) {
        
        [ShowMessage showCenterViewMessage:@"请选择行业类别"];
        return;
    }
    
    if ([self.storesTextField.text isEqualToString:@""]) {
        [ShowMessage showCenterViewMessage:@"请输入门店名称"];
        return;
    }
    
    if (self.registerType == INDIVIDUAL_BUSINESSES) {
        

        if ([self.shopInfoLabel.text isEqualToString:@"点击获取门店街道信息"]){
            [ShowMessage showCenterViewMessage:@"点击获取门店街道信息"];
            return;
        }
    }else{
        
        if ([self.shopInfoLabel.text isEqualToString:@"点击获取商场信息"]){
            [ShowMessage showCenterViewMessage:@"点击获取商场信息"];
            return;
        }
    }
    
    
    if ([self.locationTextField.text isEqualToString:@""]) {
        [ShowMessage showCenterViewMessage:self.locationTextField.placeholder];
        return;
    }
    
    [self registerWithRequestToServer];
    
}

#pragma mark -- 请求服务器
- (void)registerWithRequestToServer{
    
    [self showIndicator:@"正在注册，请稍候..."];
    
    __weak __typeof(self) weakSelf = self;
    
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:self.phoneTextField.text,@"phone",self.codeTextField.text,@"phone_code",self.pwdTextField.text,@"password",@"所属商场编号",@"fid",self.shopInfoLabel.text,@"addr",self.storesTextField.text,@"name",self.code,@"verifycode", nil];
    
    [HTTPManager post:GET_URLString(@"/Api/Member/reg") parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self hideIndicator];
        
        if ([responseObject[@"msg"] isEqualToString:@"成功"]) {
            
            [ShowMessage showCenterViewMessage:@"注册成功"];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hideIndicator];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
